package com.acertificate.domain;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.ParagraphTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class CopyOfNickname {
	
	HttpPost httppost = new HttpPost("http://dafau.sinaapp.com/share/nickname");
	
	public static final String URL_PREFIX = "http://www.qqwangming.org/article/QQwangming/wangming_002_";
	public static final String SUFFIX = ".html";

	public static int maxProcess = 4;
	public static int total = 25;
	public static int threadCount;

	{
		threadCount = total / maxProcess + (total % maxProcess == 0 ? 0 : 1);
		System.out.println(threadCount);
	}

	public static void main(String[] args) throws ParserException, MalformedURLException, Exception {
		CopyOfNickname wanmv = new CopyOfNickname();
		for (int i = 1; i <= threadCount; i++) {
			RelolThread thread = wanmv.new RelolThread((i - 1) * maxProcess + 1, (i * maxProcess > total ? total : i * maxProcess));
			thread.start();
		}
	}

	class RelolThread extends Thread {

		public int start;
		public int end;

		public RelolThread() {
		}

		public RelolThread(int start, int end) {
			this.start = start;
			this.end = end;
		}

		public void run() {
			for (int i = start; i < end; i++) {
				getManifest(i, URL_PREFIX + i + SUFFIX);
			}
		}

		public List<String> getManifest(int x, String url) {
			System.out.println(url);
			HttpClient httpclient = new DefaultHttpClient();
			List<String> results = new ArrayList<String>();
			try {
				URLConnection urlConnection = new URL(url).openConnection();
				String content1 = IOUtils.toString(urlConnection.getInputStream());
				Parser contentParser = new Parser();
				contentParser.setInputHTML(content1);

				AndFilter andFilter = new AndFilter(new NodeClassFilter(LinkTag.class), new HasAttributeFilter("class", "showtopic"));
				NodeList xx = contentParser.extractAllNodesThatMatch(andFilter);
				for (int i = 0; i < xx.size(); i++) {
					LinkTag node = (LinkTag)xx.elementAt(i);
					if(!node.getLink().contains("qqwangming.org")){
						List<String> results1 = new ArrayList<String>();
						try {
							URLConnection urlConnection1 = new URL("http://www.qqwangming.org" + node.getLink()).openConnection();
							String content11 = IOUtils.toString(urlConnection1.getInputStream());
							Parser contentParser1 = new Parser();
							contentParser1.setInputHTML(content11);

							AndFilter andFilter1 = new AndFilter(new NodeClassFilter(ParagraphTag.class), new HasAttributeFilter("align", "center"));
							NodeList xx1 = contentParser1.extractAllNodesThatMatch(andFilter1);
							for (int i1 = 0; i1 < xx1.size(); i1++) {
								Node node1 = xx1.elementAt(i1);
								String txt1 = node1.toPlainTextString();
								if(txt1.length() < 2)
									continue;
								txt1 = txt1.trim().replace(" ", "");
								txt1 = txt1.replace("\\", "\\ ");
								txt1 = txt1.replace("\n", "");
								txt1 = txt1.replace("\n\r", "");
//								txt = txt.replace("\"", "\"");
								txt1 = txt1.replace("'", "\\'");
//								results.add(txt1);
								MultipartEntity reqEntity = new MultipartEntity();

								StringBody field1 = new StringBody(txt1.replace("&nbsp;", "").trim(),
										Charset.forName("UTF-8"));
								
								reqEntity.addPart("field1", field1);
								httppost.setEntity(reqEntity);

								HttpResponse response = httpclient.execute(httppost);
								HttpEntity resEntity = response.getEntity();

								if (resEntity != null) {
									System.out.println(IOUtils.toString(resEntity.getContent()));
								}
								EntityUtils.consume(resEntity);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			return results;
		}
	}

	class Entity {
		private int id;
		private String en;

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getEn() {
			return en;
		}

		public void setEn(String en) {
			this.en = en;
		}

		public String getTime() {
			return time;
		}

		public void setTime(String time) {
			this.time = time;
		}

		private String time;
	}

}
